package com.chezhujie.www.core;

import java.util.Set;

import com.chezhujie.www.domain.Trolepermission;
import com.chezhujie.www.domain.Tsuser;
import com.chezhujie.www.domain.vo.TUser;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import org.springframework.web.util.UrlPathHelper;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by Administrator on 2017/3/19.
 */

public class LoginInterceptor extends HandlerInterceptorAdapter  {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("preHandle:"+request.getRequestURI());
        if(checkLogin(request)){
            // 获得用户
        	Tsuser user =  (Tsuser) request.getSession().getAttribute("user");
            // 用户为null跳转到登陆页面
            if (user == null) {
                response.sendRedirect(request.getContextPath()+"/admin/login.do");
                return false;
            }else{
            	//没有权限跳转页面登陆页面
            	if(!uriPer(request,user)){
            	  response.sendRedirect(request.getContextPath()+"/cal/tuserPer.do");
            	  return false;
            	}
            }
        }
        return true;
    }
    
    private boolean uriPer(HttpServletRequest request,Tsuser user){
    	boolean index =false;
    	String uri = getURI(request);
        Set<Trolepermission> set = user.getTrolepermissions();
        for(Trolepermission trolepermission : set){
        	if(trolepermission.getUri().equals(uri)){
        		index = true;
        	}
        }
    	
    	return index;
    }

    private boolean checkLogin(HttpServletRequest request) {

        String uri = getURI(request);
        for (String s : excludeURL) {
            if(s.equals(uri)||uri.startsWith(s)){
                return false;
            }
        }

        for (String s : includeURL) {
            if(s.equals(uri)||uri.startsWith(s)){
                return true;
            }
        }
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
//        System.out.println("postHandle");
        super.postHandle(request, response, handler, modelAndView);
    }
    private String[] includeURL;
    private String[] excludeURL;

    public void setExcludeURL(String[] excludeURL) {
        this.excludeURL = excludeURL;
    }

    public void setIncludeURL(String[] includeURL) {
        this.includeURL = includeURL;
    }
    /**
     * 获得第三个路径分隔符的位置
     *
     * @param request
     * @throws IllegalStateException
     *             访问路径错误，没有三(四)个'/'
     */
    private static String getURI(HttpServletRequest request)
            throws IllegalStateException {

        UrlPathHelper helper = new UrlPathHelper();
        String uri = helper.getOriginatingRequestUri(request);
        String ctxPath = helper.getOriginatingContextPath(request);
        return uri.replace(ctxPath,"");

    }
}
